package queue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class MyBlockingQueue2 {
    public static void main(String[] args) {
        SynchronousQueue<String> queue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+" 插入1");
                queue.put("1");

                System.out.println(Thread.currentThread().getName()+" 插入2");
                queue.put("2");

                System.out.println(Thread.currentThread().getName()+" 插入3");
                queue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"PUT线程").start();


        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" 取出: "+queue.take());

                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" 取出: "+queue.take());

                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+" 取出: "+queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"Take线程").start();
    }
}
/*
PUT线程 插入1
Take线程 取出: 1
PUT线程 插入2
 */